
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>模型 _meta API &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="关联对象参考" href="relations.html" />
    <link rel="prev" title="约束参考" href="constraints.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="constraints.html" title="约束参考">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="relations.html" title="关联对象参考">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-meta">
            
  <div class="section" id="s-module-django.db.models.options">
<span id="s-model-meta-api"></span><span id="module-django.db.models.options"></span><span id="model-meta-api"></span><h1>模型 <code class="docutils literal notranslate"><span class="pre">_meta</span></code> API<a class="headerlink" href="#module-django.db.models.options" title="永久链接至标题">¶</a></h1>
<dl class="class">
<dt id="django.db.models.options.Options">
<em class="property">class </em><code class="descname">Options</code><a class="headerlink" href="#django.db.models.options.Options" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>模型 <code class="docutils literal notranslate"><span class="pre">_meta</span></code> 的 API 是 Django ORM 的核心。它使系统的其他部分，如查找、查询、表单和管理能够理解每个模型的功能。API 可以通过每个模型类的 <code class="docutils literal notranslate"><span class="pre">_meta</span></code> 属性来访问，它是 <code class="docutils literal notranslate"><span class="pre">django.db.models.options.Options</span></code> 对象的一个实例。</p>
<p>它提供的方法可以用来：</p>
<ul class="simple">
<li>检索一个模型的所有字段实例</li>
<li>按名称检索模型的单个字段实例</li>
</ul>
<div class="section" id="s-field-access-api">
<span id="s-model-meta-field-api"></span><span id="field-access-api"></span><span id="model-meta-field-api"></span><h2>字段访问 API<a class="headerlink" href="#field-access-api" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-retrieving-a-single-field-instance-of-a-model-by-name">
<span id="retrieving-a-single-field-instance-of-a-model-by-name"></span><h3>按名称检索模型的单个字段实例<a class="headerlink" href="#retrieving-a-single-field-instance-of-a-model-by-name" title="永久链接至标题">¶</a></h3>
<dl class="method">
<dt id="django.db.models.options.Options.get_field">
<code class="descclassname">Options.</code><code class="descname">get_field</code>(<em>field_name</em>)<a class="headerlink" href="#django.db.models.options.Options.get_field" title="永久链接至目标">¶</a></dt>
<dd><p>返回给定字段名的字段实例。</p>
<p><code class="docutils literal notranslate"><span class="pre">field_name</span></code> 可以是模型上的字段名，也可以是抽象或继承的模型上的字段名，或者是指向模型的另一个模型上定义的字段名。在后一种情况下，<code class="docutils literal notranslate"><span class="pre">field_name</span></code> 将是（按优先级排序）用户设置的 <a class="reference internal" href="fields.html#django.db.models.ForeignKey.related_query_name" title="django.db.models.ForeignKey.related_query_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">related_query_name</span></code></a>，用户设置的 <a class="reference internal" href="fields.html#django.db.models.ForeignKey.related_name" title="django.db.models.ForeignKey.related_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">related_name</span></code></a>，或者 Django 自动生成的名称。</p>
<p><a class="reference internal" href="fields.html#django.db.models.Field.hidden" title="django.db.models.Field.hidden"><code class="xref py py-attr docutils literal notranslate"><span class="pre">隐藏字段</span></code></a> 不能按名称检索。</p>
<p>如果没有找到指定名称的字段，将引发一个 <a class="reference internal" href="../exceptions.html#django.core.exceptions.FieldDoesNotExist" title="django.core.exceptions.FieldDoesNotExist"><code class="xref py py-class docutils literal notranslate"><span class="pre">FieldDoesNotExist</span></code></a> 异常。</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>

<span class="go"># A field on the model</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">)</span>
<span class="go">&lt;django.db.models.fields.CharField: username&gt;</span>

<span class="go"># A field from another model that has a relation with the current model</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="s1">&#39;logentry&#39;</span><span class="p">)</span>
<span class="go">&lt;ManyToOneRel: admin.logentry&gt;</span>

<span class="go"># A non existent field</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="s1">&#39;does_not_exist&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">FieldDoesNotExist</span>: <span class="n">User has no field named &#39;does_not_exist&#39;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-retrieving-all-field-instances-of-a-model">
<span id="retrieving-all-field-instances-of-a-model"></span><h3>检索一个模型的所有字段实例<a class="headerlink" href="#retrieving-all-field-instances-of-a-model" title="永久链接至标题">¶</a></h3>
<dl class="method">
<dt id="django.db.models.options.Options.get_fields">
<code class="descclassname">Options.</code><code class="descname">get_fields</code>(<em>include_parents=True</em>, <em>include_hidden=False</em>)<a class="headerlink" href="#django.db.models.options.Options.get_fields" title="永久链接至目标">¶</a></dt>
<dd><p>返回与模型相关联的字段的元组。<code class="docutils literal notranslate"><span class="pre">get_fields()</span></code> 接受两个参数，可以用来控制返回哪些字段：</p>
<dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">include_parents</span></code></dt>
<dd>默认为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。递归地包括父类上定义的字段。如果设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>，<code class="docutils literal notranslate"><span class="pre">get_fields()</span></code> 将只搜索直接在当前模型上声明的字段。直接继承自抽象模型或代理类的模型的字段被认为是本地的，而不是在父类上。</dd>
<dt><code class="docutils literal notranslate"><span class="pre">include_hidden</span></code></dt>
<dd>默认为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。如果设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，<code class="docutils literal notranslate"><span class="pre">get_fields()</span></code> 将包括用于支持其他字段功能的字段。这也将包括任何具有 <code class="docutils literal notranslate"><span class="pre">related_name</span></code> 的字段（如 <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> 或 <a class="reference internal" href="fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">ForeignKey</span></code></a>），并以 “+” 开头。</dd>
</dl>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_fields</span><span class="p">()</span>
<span class="go">(&lt;ManyToOneRel: admin.logentry&gt;,</span>
<span class="go"> &lt;django.db.models.fields.AutoField: id&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: password&gt;,</span>
<span class="go"> &lt;django.db.models.fields.DateTimeField: last_login&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_superuser&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: username&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: first_name&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: last_name&gt;,</span>
<span class="go"> &lt;django.db.models.fields.EmailField: email&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_staff&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_active&gt;,</span>
<span class="go"> &lt;django.db.models.fields.DateTimeField: date_joined&gt;,</span>
<span class="go"> &lt;django.db.models.fields.related.ManyToManyField: groups&gt;,</span>
<span class="go"> &lt;django.db.models.fields.related.ManyToManyField: user_permissions&gt;)</span>

<span class="go"># Also include hidden fields.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">User</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_fields</span><span class="p">(</span><span class="n">include_hidden</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(&lt;ManyToOneRel: auth.user_groups&gt;,</span>
<span class="go"> &lt;ManyToOneRel: auth.user_user_permissions&gt;,</span>
<span class="go"> &lt;ManyToOneRel: admin.logentry&gt;,</span>
<span class="go"> &lt;django.db.models.fields.AutoField: id&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: password&gt;,</span>
<span class="go"> &lt;django.db.models.fields.DateTimeField: last_login&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_superuser&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: username&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: first_name&gt;,</span>
<span class="go"> &lt;django.db.models.fields.CharField: last_name&gt;,</span>
<span class="go"> &lt;django.db.models.fields.EmailField: email&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_staff&gt;,</span>
<span class="go"> &lt;django.db.models.fields.BooleanField: is_active&gt;,</span>
<span class="go"> &lt;django.db.models.fields.DateTimeField: date_joined&gt;,</span>
<span class="go"> &lt;django.db.models.fields.related.ManyToManyField: groups&gt;,</span>
<span class="go"> &lt;django.db.models.fields.related.ManyToManyField: user_permissions&gt;)</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">模型 <code class="docutils literal notranslate"><span class="pre">_meta</span></code> API</a><ul>
<li><a class="reference internal" href="#field-access-api">字段访问 API</a><ul>
<li><a class="reference internal" href="#retrieving-a-single-field-instance-of-a-model-by-name">按名称检索模型的单个字段实例</a></li>
<li><a class="reference internal" href="#retrieving-all-field-instances-of-a-model">检索一个模型的所有字段实例</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="constraints.html"
                        title="上一章">约束参考</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="relations.html"
                        title="下一章">关联对象参考</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/models/meta.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="constraints.html" title="约束参考">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="relations.html" title="关联对象参考">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>